<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 仓库管理
 * @package app\Store
 * @author LJQ<245369209@qq.com>
 */
class Store extends Api

{

    /**
   *仓库列表
   * @param string enterpriseId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function storeList($scope = null){
		$where = [] ;
		if($scope){
			
			$where['enterpriseId'] = ['in',$scope];
		}
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//仓库列表
			$info =collection(model('Store')->with('enterprise,staff')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//仓库列表
			$info =collection(model('Store')->with('enterprise,staff')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Store')->where($where)->count('id'); 
		
    		if ($count == 0) {
         		return json(['code'=>1,'msg'=>'没有找到仓库','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'仓库信息','count'=>$count,'data'=>$info]);
       		}
            return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
   	      //添加仓库
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('Basic/Store');
          $result = $model->change($data);
          if (false != $result) {
            setlog('Storeadd','Store',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'仓库添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'仓库添加失败,请检查']);
          }
      }

   	

   /**
   * 修改仓库
   * @param  string storeId 仓库id,
   * @return Json
   */
   	public function edit($data,$storeId){
        
          if (empty($storeId)) {
              return json(['code'=>0,'msg'=>'仓库不存在,请检查']);
          }
          $model = model('Basic/Store');     
          if ($model->change($data)) {
            setlog('Storeupdate','Store',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'仓库信息修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'仓库信息修改失败,请检查']);
            }
    }
    /**
   *  删除仓库
   * @param string storeId 仓库id,
   * @param Json
   */
     
    public function del($storeId) {
        $model = model('Basic/Store');
      	if (empty ( $storeId )||empty(($model->where(array('id'=>$storeId))->find()))) {
           	return  json(['code'=>0,'msg'=>'仓库id不存在,请检查']);
        }
        if($model->destroy(['id'=>$storeId]) !== FALSE){
          setlog('Storedel','Store',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'仓库删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'仓库删除失败,请检查']);
       }
    }



      /**
   *   获得企业列表
   * @return Json
   */

      public function getEnterprise() {
           $enterprise = model('basic/Enterprise');
           $enterprsies = collection($enterprise->field('id,fullName')->order('id')->select())->toArray();
           $enterprsies[] = array_unshift($enterprsies, ['id'=>0 , 'fullName'=>'管委会']);
           return json(['code'=>1,'msg'=>'企业列表','data'=>$enterprsies]);
    }

	/**
	 * 获取仓库位置
	 * @param string $storeId
	 * @return Json
	 */
	public function getStorePosition($storeId){
		$position = model('store')->where('id',$storeId)->value('latitudeLongitude');
		if($position){
			return json(['code'=>1,'msg'=>'','data'=>$position]);
		}else{
			return json(['code'=>0,'msg'=>'没有对应位置']);
		}
	}

     
   /**
   *   查询仓库
   * @param string enterpriseId 企业id
   * @param keyword仓库名称
   * @return Json
   */
   	public function search($store = null,$enterpriseId=0){
   		$where = [] ;

		if($enterpriseId!=0){
			
			$where['enterpriseId'] = ['eq',$enterpriseId];
		}

		if($store){
			$where['storeName'] = ['like','%'.$store.'%'];
		}

       
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//仓库列表
			$info =collection(model('Store')->with('enterprise,staff')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//仓库列表
			$info =collection(model('Store')->with('enterprise,staff')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('Store')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到仓库','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'仓库信息','count'=>$count,'data'=>$info]);
       	}
      }
}